//
//  ViewController.m
//  队列管理demo
//
//  Created by 张闯 on 16/2/20.
//  Copyright © 2016年 张闯的mac pro. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
    operationQueue.name = @"给熊孩纸做的demo";
    //设置最大对别并发数
    //1.如果没有设置最大并发数，那么并发的个数是由系统内存和CPU决定的，可能内存多久开多一点，内存少就开少一点。
    //2.最大并发数不要乱写（5以内），不要开太多，一般以2~3为宜，因为虽然任务是在子线程进行处理的，但是cpu处理这些过多的子线程可能会影响UI，让UI变卡。
    [operationQueue setMaxConcurrentOperationCount:2];
    
//    NSOperation *opration1 = [[NSOperation alloc] init];
//    NSOperation *opration2 = [[NSOperation alloc] init];
    NSInvocationOperation *operation1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test_One) object:nil];
    operation1.name = @"thread1";
    
    NSInvocationOperation *operation2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(test_Two) object:nil];
    operation2.name = @"thread2";

    //设置线程优先级
   // operation1.queuePriority = NSOperationQueuePriorityVeryHigh;
    //operation2.queuePriority = NSOperationQueuePriorityLow;
    
    
    //执行顺序
    //不能是相互依赖
//    [operation2 addDependency:operation1];
    
    //添加到operationQueue
#pragma mark - 并发执行的线程 是随机的
    [operationQueue addOperation:operation2];
    [operationQueue addOperation:operation1];
    
    
    //取消
//    [operationQueue cancelAllOperations];
    
    NSLog(@"op1_____:%d,/n op2____:%d", operation1.cancelled, operation2.cancelled);
    
    //暂停
    //暂停和恢复的适用场合：在tableview界面，开线程下载远程的网络界面，对UI会有影响，使用户体验变差。那么这种情况，就可以设置在用户操作UI（如滚动屏幕）的时候，暂停队列（不是取消队列），停止滚动的时候，恢复队列。
//    operationQueue.suspended = YES;
    
    //查看
    NSArray *array = [operationQueue operations];
    NSLog(@"operationQueue_____%@%@", operationQueue.name, array);
    
    NSLog(@"dadafd");
    

    
}


- (void)test_One
{
    NSLog(@"one:%@", [NSThread currentThread]);
}

- (void)test_Two
{
    NSLog(@"two:%@", [NSThread currentThread]);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
